
package pimp.organizermanager;

import java.util.ArrayList;

public class PNode extends Node{

    private ArrayList<GlobalEvent> globalEvents = null;  

    public PNode(String name) {
        super(name);
        globalEvents = new ArrayList<GlobalEvent>();
    }

    public ArrayList<GlobalEvent> getGlobalEvents(){
        return globalEvents;
    }

    int getUnusedUniqueID() {

        //Todo: Fix this! Very ugly, slow implementation (n^2)!
        int ret=0;
        for(int i=0, n=globalEvents.size(); i<n; i++)
        {
            if( globalEvents.get(i).getId().getUniqueID() == ret )
            {
                ret++;
                i=0;
            }
        }

        return ret;
    }

    @Override
    Node transformToNode(){
        Node ret = new Node(name);

        //przygotowanie globalEventów i schowanie ich do poddrzewa, by nie pomieszały się ze zwykłymi Node'ami
        Node glEvs = new Node("List");
        for(GlobalEvent g: globalEvents)
            glEvs.addChild(g.transformToNode());

        ret.addChild(glEvs);


        for(int i=0, n = super.childs.size(); i<n ; i++)
        {
            PNode tmp = (PNode)childs.get(i);
            ret.addChild(tmp.transformToNode());
        }

        return ret;
    }

    public boolean addGlobalEvent(GlobalEvent globalEvent)
    {
        globalEvents.add(globalEvent);
        return true;
    }

}
